/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindow.java
 *
 * Created on 30/04/2010, 00:08:39
 */
package controlenotas;

import DAO.Dados;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;
import negocio.Aluno;
import negocio.Turma;
import org.pushingpixels.flamingo.api.common.JCommandButton;
import org.pushingpixels.flamingo.api.common.JCommandToggleButton;
import org.pushingpixels.flamingo.api.common.icon.ImageWrapperResizableIcon;
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.JRibbonFrame;
import org.pushingpixels.flamingo.api.ribbon.RibbonApplicationMenu;
import org.pushingpixels.flamingo.api.ribbon.RibbonApplicationMenuEntryFooter;
import org.pushingpixels.flamingo.api.ribbon.RibbonElementPriority;
import org.pushingpixels.flamingo.api.ribbon.RibbonTask;
import org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies;

/**
 *
 * @author jeferson
 */
public class MainWindow extends JRibbonFrame {
    
    private TurmaTableModel ttm;
    private turmascbmodel tcbm;
    private Dados dados = new Dados("Colégio Estadual Governador Valadares", "jeferson");
    private File f;
    private Pdf pdf;
    private File savefile = null;
    private File openfile = null;
    
    /** Creates new form MainWindow */
    public MainWindow() {
        ttm = new TurmaTableModel(dados.getTurma(0));
        tcbm = new turmascbmodel(dados);
        initComponents();
        this.setSize(800, 600);
        this.setLocationRelativeTo(null);
        this.setTitle("Controle de Notas");
        jXTitledPanel1.setTitle(dados.getProfessor());
        TableColumnModel tcm = jXTable1.getColumnModel();
        tcm.getColumn(0).setPreferredWidth(5);
        tcm.getColumn(1).setPreferredWidth(500);
        for (int i = 0; i < 15; i++) {
            if (i > 1) {
                tcm.getColumn(i).setPreferredWidth(20);
            }
            tcm.getColumn(i).setCellRenderer(new MCellRender());
        }
        jXTable1.setGridColor(Color.gray);
        jXTable1.setShowGrid(true);
        DefaultTableCellRenderer header = (DefaultTableCellRenderer) jXTable1.getTableHeader().getDefaultRenderer();
        header.setHorizontalAlignment(SwingConstants.CENTER);
        /*TableColumnModel cm = jXTable1.getColumnModel();
        ColumnGroup g_name = new ColumnGroup("1º Semestre");
        g_name.add(cm.getColumn(2));
        g_name.add(cm.getColumn(3));
        ColumnGroup g_lang = new ColumnGroup("2º Semestre");
        g_lang.add(cm.getColumn(4));
        g_lang.add(cm.getColumn(5));*/
        /*ColumnGroup g_other = new ColumnGroup("Others");
        g_other.add(cm.getColumn(4));
        g_other.add(cm.getColumn(5));
        g_lang.add(g_other);*/

        /*GroupableTableHeader h = new GroupableTableHeader(jXTable1.getColumnModel());
        jXTable1.setTableHeader(h);
        h.addColumnGroup(g_name);
        h.addColumnGroup(g_lang);*/
        //JScrollPane scroll = new JScrollPane(table);
       // getContentPane().add(scroll);
        //setSize(400, 120);
        createRibbonPanel();
    }
    
    public void addAluno(Turma t, Aluno a){
        t.addAluno(a);
        ttm.setTurma(t);
        jXTable1.repaint();
    }
    
    private void createRibbonPanel(){
        addRibbonTaskBar();
        this.getRibbon().configureHelp(getResizableIconFromResource("resources/help_32.png"), 
                new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(rootPane, "Teste");
            }
        });
        this.getRibbon().addTask(getFileTask());
        this.getRibbon().addTask(getConfiguracaoTask());
        this.getRibbon().addTask(getViewTask());
        this.getRibbon().addTask(getRelatorioTask());
        this.createApplicationMenu();
    }
    
    private void createApplicationMenu(){
        RibbonApplicationMenu am = new RibbonApplicationMenu();
        ResizableIcon iri = getResizableIconFromResource("resources/class_32.png");//new ImageResizableIcon(ii);
        am.addFooterEntry(new RibbonApplicationMenuEntryFooter(iri, "teste", null));
        this.getRibbon().setApplicationMenu(am);
        this.setApplicationIcon(iri);
    }
    
    private RibbonTask getFileTask(){
        RibbonTask file = new RibbonTask("Arquivo", getFileBand(), getImportBand(), getExportBand());
        return file;
    }
    
    private JRibbonBand getFileBand(){
        JRibbonBand fileBand = new JRibbonBand("Arquivo", null);
        addCommandButtond("Novo", "resources/new_64.png", fileBand, RibbonElementPriority.TOP, null);
        ActionListener act = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                abrirButtonActionPerformed(e);
            }
        };
        addCommandButtond("Abrir", "resources/open_48.png", fileBand, RibbonElementPriority.MEDIUM, act);
        ActionListener act1 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                salvarButtonActionPerformed(e);
            }
        };
        addCommandButtond("Salvar", "resources/save_32.png", fileBand, RibbonElementPriority.MEDIUM, act1);
        addCommandButtond("Salvar Como", "resources/saveas_32.png", fileBand, RibbonElementPriority.MEDIUM, null);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(fileBand.getControlPanel()));
        fileBand.setResizePolicies(polices1);
        return fileBand;
    }
    
    private void abrirButtonActionPerformed(ActionEvent evt) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".not") || f.isDirectory();
            }

            public String getDescription() {
                return "Arquivos de Notas (.not)";
            }
        });
        chooser.showOpenDialog(this);
        openfile = chooser.getSelectedFile();
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(openfile));
            this.dados = (Dados) ois.readObject();
            //System.out.println(""+dados.getTurmas().size());
            tcbm = new turmascbmodel(dados);
            jComboBox2.setModel(tcbm);
            ois.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Dados.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Dados.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                ois.close();
            } catch (IOException ex) {
                Logger.getLogger(Dados.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        tcbm.changed();
        if(jComboBox2.getItemCount()>0)
            jComboBox2.setSelectedIndex(0);
    }

    public void addTurma(Turma turma) {
        dados.addTurma(turma);
        tcbm.changed();
        jComboBox2.setSelectedIndex(0);
        //ttm.fireTableDataChanged();
    }

    /*public void addAluno(Turma t, Aluno a) {
        t.addAluno(a);
    }*/
    
    private void salvarButtonActionPerformed(ActionEvent evt) {
        if(savefile==null){
            JFileChooser chooser = new JFileChooser();
            chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

                public boolean accept(File f) {
                    return f.getName().toLowerCase().endsWith(".not") || f.isDirectory();
                }

                public String getDescription() {
                    return "Arquivos de Nota (.not)";
                }
            });
            chooser.showSaveDialog(this);
            savefile = chooser.getSelectedFile();
        }
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(savefile));
            oos.writeObject(dados);
            oos.close();
        } catch (IOException ex) {
            Logger.getLogger(Dados.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                oos.close();
            } catch (IOException ex) {
                Logger.getLogger(Dados.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    private JRibbonBand getImportBand(){
        JRibbonBand band = new JRibbonBand("Import", null);
        ActionListener act = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                importarAlunosTxt();
            }
        };
        addCommandButtond("Alunos (.txt)", "resources/import_32.png", band, RibbonElementPriority.TOP, act);
        addCommandButtond("Alunos (.pdf)", "resources/import_32.png", band, RibbonElementPriority.MEDIUM, null);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private void importarAlunosTxt(){
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".txt") || f.isDirectory();
            }

            public String getDescription() {
                return "Arquivos de Texto (.txt)";
            }
        });
        chooser.showOpenDialog(this);
        f = chooser.getSelectedFile();
        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(f));
            while(br.ready()){
                String s1 = br.readLine();
                if(s1.equals(""))
                    break;
                int s = Integer.parseInt(s1);
                String t = br.readLine();
                String linha = br.readLine();
                Turma turma = new Turma(s, t);
                int n = 1;
                while(!linha.equals("|")){
                    Aluno a = new Aluno(n, linha, turma);
                    n++;
                    turma.addAluno(a);
                    linha = br.readLine();
                }
                addTurma(turma);
            }
            br.close();
            this.repaint();
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private JRibbonBand getExportBand(){
        ActionListener act = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                gerarListasPresenca("Colégio Estadual Governador Valadares", 
            "Josineide", "Resultado Final" );
            }
        };
        ActionListener act1 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                gerarListasNotas("Colégio Estadual Governador Valadares", 
            "Jeferson", "4ª Avaliação de Química" );
            }
        };
        ActionListener act2 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                gerarResultadoFinal();
            }
        };
        ActionListener act3 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                gerarRec2sem();
            }
        };
        ActionListener act4 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                gerarResRec2sem();
            }
        };
        JRibbonBand band = new JRibbonBand("Export", null);
        addCommandButtond("Listas de Presença (.pdf)", "resources/export_32.png", band, RibbonElementPriority.TOP, act);
        addCommandButtond("Notas (.pdf)", "resources/export_32.png", band, RibbonElementPriority.MEDIUM, act1);
        addCommandButtond("Resultado Final (.pdf)", "resources/export_32.png", band, RibbonElementPriority.MEDIUM, act2);
        addCommandButtond("Alunos em Recuperação 2º Semestre (.pdf)", "resources/export_32.png", band, RibbonElementPriority.MEDIUM, act3);
        addCommandButtond("Recuperação 2º Semestre (.pdf)", "resources/export_32.png", band, RibbonElementPriority.MEDIUM, act4);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private void gerarRec2sem(){
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".pdf") || f.isDirectory();
            }

            public String getDescription() {
                return "Portable Document Format (.pdf)";
            }
        });
        chooser.showSaveDialog(this);
        try {
            ArrayList<Turma> tms = dados.getTurmas();
            File nf = chooser.getSelectedFile();
            if (nf != null) {
                pdf = new Pdf(nf);
            }
            FileOutputStream fos = new FileOutputStream(nf);
            Document doc = new Document(PageSize.A4, 20, 20, 15, 1);
            PdfWriter.getInstance(doc, fos);
            doc.open();
            doc.addTitle("Alunos em Recuperação no 2º Semestre");
            final int col = 4;
            //int lines = 45;
            Turma t;
            for (int i = 0; i < tms.size(); i++) {
                doc.newPage();
                t=tms.get(i);
                Chunk c = new Chunk("GOVERNO DE SERGIPE", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                Paragraph parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("SECRETARIA DE ESTADO DA EDUCAÇÃO", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("COLÉGIO ESTADUAL GOVERNADOR VALADARES", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("FREQUÊNCIA DA RECUPERAÇÃO DO 2º SEMESTRE DA TURMA " + t.getNome(), new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("Professor: Jeferson", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                ArrayList<Aluno> alunos = t.getArrayAlunos();
                float[] l = {2f, 32f, 30f, 5f};
                PdfPTable table = new PdfPTable(l);
                table.setWidthPercentage(100f);
                Phrase p = new Phrase(new Chunk("Nº", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                PdfPCell cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cell.setBorder(PdfPCell.TOP);
                table.addCell(cell);
                p = new Phrase(new Chunk("ALUNO", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.addCell(cell);
                p = new Phrase(new Chunk("ASSINATURA", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.addCell(cell);
                p = new Phrase(new Chunk("NOTA", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cell.setBorder(PdfPCell.TOP);
                table.addCell(cell);
                int lines = getNumAlunosRec2Sem(alunos);
                //JOptionPane.showMessageDialog(rootPane, t.getNome()+": "+lines);
                for (int j = 0; j < alunos.size(); j++) {
                    if (alunos.get(j).isRec2S()) {
                        p = new Phrase(new Chunk("" + alunos.get(j).getNum(), new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setBorder(PdfPCell.TOP);
                        table.addCell(cell);
                        p = new Phrase(new Chunk(alunos.get(j).getNome(), new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        table.addCell(cell);
                        p = new Phrase(new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        if (j == lines - 1) {
                            cell.setBorder(PdfPCell.TOP);
                        }
                        for (int k = 0; k < col - 3; k++) {
                            table.addCell(cell);
                        }
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setBorder(PdfPCell.TOP);
                        table.addCell(cell);
                    }

                }
                doc.add(table);
            }
            doc.close();
        } catch (DocumentException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
    private void gerarResRec2sem(){
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".pdf") || f.isDirectory();
            }

            public String getDescription() {
                return "Portable Document Format (.pdf)";
            }
        });
        chooser.showSaveDialog(this);
        try {
            ArrayList<Turma> tms = dados.getTurmas();
            File nf = chooser.getSelectedFile();
            if (nf != null) {
                pdf = new Pdf(nf);
            }
            FileOutputStream fos = new FileOutputStream(nf);
            Document doc = new Document(PageSize.A4, 20, 20, 15, 1);
            PdfWriter.getInstance(doc, fos);
            doc.open();
            doc.addTitle("Alunos em Recuperação no 2º Semestre");
            final int col = 5;
            //int lines = 45;
            Turma t;
            for (int i = 0; i < tms.size(); i++) {
                doc.newPage();
                t=tms.get(i);
                Chunk c = new Chunk("GOVERNO DE SERGIPE", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                Paragraph parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("SECRETARIA DE ESTADO DA EDUCAÇÃO", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("COLÉGIO ESTADUAL GOVERNADOR VALADARES", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("FREQUÊNCIA DA RECUPERAÇÃO DO 2º SEMESTRE DA TURMA " + t.getNome(), new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("Professor: Jeferson", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                ArrayList<Aluno> alunos = t.getArrayAlunos();
                float[] l = {2f, 32f, 5f, 5f, 5f};
                PdfPTable table = new PdfPTable(l);
                table.setWidthPercentage(100f);
                Phrase p = new Phrase(new Chunk("Nº", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                PdfPCell cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cell.setBorder(PdfPCell.TOP);
                table.addCell(cell);
                p = new Phrase(new Chunk("ALUNO", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.addCell(cell);
                p = new Phrase(new Chunk("NR2S", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.addCell(cell);
                p = new Phrase(new Chunk("MF", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cell.setBorder(PdfPCell.TOP);
                table.addCell(cell);
                p = new Phrase(new Chunk("RF", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                cell.setBorder(PdfPCell.TOP);
                table.addCell(cell);
                int lines = alunos.size();
                //JOptionPane.showMessageDialog(rootPane, t.getNome()+": "+lines);
                for (int j = 0; j < alunos.size(); j++) {
                    //if (alunos.get(j).isRec2Sem()) {
                        p = new Phrase(new Chunk("" + alunos.get(j).getNum(), new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setBorder(PdfPCell.TOP);
                        table.addCell(cell);
                        p = new Phrase(new Chunk(alunos.get(j).getNome(), new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        table.addCell(cell);
                        String n2r = alunos.get(j).getNotaInString("R2");
                        p = new Phrase(new Chunk(n2r, new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        table.addCell(cell);
                        //BigDecimal mfp = new BigDecimal(alunos.get(j).getMediaFinalParcial());
                        p = new Phrase(new Chunk(alunos.get(j).getNotaInString("NP"), new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        table.addCell(cell);
                        if(alunos.get(j).isRecF()){
                            p = new Phrase(new Chunk("", new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        }else{
                            p = new Phrase(new Chunk("X", new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        }
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        table.addCell(cell);
                        if (j == lines - 1) {
                            cell.setBorder(PdfPCell.TOP);
                            cell = new PdfPCell(p);
                            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                            cell.setBorder(PdfPCell.TOP);
                            table.addCell(cell);
                        }
                       /* for (int k = 0; k < col - 3; k++) {
                                table.addCell(cell);
                        }*/
                        
                    //}
                }
                doc.add(table);
            }
            doc.close();
        } catch (DocumentException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private int getNumAlunosRec2Sem(ArrayList<Aluno> alunos) {
        int result = 0;
        for (Aluno a : alunos) {
            if (a.isRec2S()) {
                result++;
            }
        }
        return result;
    }
    
    private void gerarResultadoFinal(){
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".pdf") || f.isDirectory();
            }

            public String getDescription() {
                return "Portable Document Format (.pdf)";
            }
        });

        BaseColor bc = new BaseColor(new Color(220, 220, 220));

        chooser.showSaveDialog(this);
        File nf = chooser.getSelectedFile();
        if (nf != null) {
            pdf = new Pdf(nf);
            ArrayList<Turma> tms = dados.getTurmas();
            for(int i=0; i<tms.size(); i++){
                pdf.addPage();
                pdf.setFontSize(16);
                pdf.setFontStyle(Font.BOLD);
                pdf.createCabecalho("Colégio Estadual Governador Valadares", "Química", "Jeferson", tms.get(i).getNome());
                pdf.setFontSize(10);
                pdf.setFontStyle(Font.NORMAL);
                int n = 7;
                //int[] widths = {5, 75, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
                int[] widths = {5, 75, 8, 8, 8, 8, 8};
                pdf.createTable(n, widths);
                String[] head = {"Nº", "NOME", "M1S", "M2", "MF", "RS", "SIT"};
                int[] vAlign = new int[n];//{Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER};
                int[] hAlign = new int[n];//{Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER};
                Font fonte = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
                Font bfonte = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD);
                Font[] fts = new Font[n];//{bfonte, bfonte, bfonte, bfonte, bfonte, bfonte, bfonte};
                BaseColor[] bgc = new BaseColor[n];//{BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY};
                BaseColor[] bgColor1 = new BaseColor[n];//{BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, bc, BaseColor.WHITE, BaseColor.WHITE};
                BaseColor[] bgColor3 = new BaseColor[n];//{BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE};
                for(int k=0; k<n; k++){
                    vAlign[k] = Element.ALIGN_CENTER;
                    hAlign[k] = Element.ALIGN_CENTER;
                    fts[k] = bfonte;
                    bgc[k] = BaseColor.GRAY;
                    bgColor1[k] = BaseColor.WHITE;
                    bgColor3[k] = BaseColor.WHITE;
                }
                BaseColor[] bgColor2 = {bc, bc, bc, bc, BaseColor.LIGHT_GRAY, bc, BaseColor.LIGHT_GRAY};
                int[] border = {PdfPCell.TOP, PdfPCell.BOX, PdfPCell.BOX, PdfPCell.BOX, PdfPCell.BOX, PdfPCell.BOX, PdfPCell.BOX};
                pdf.addTableRow(head, vAlign, hAlign, bgc, border, fts);
                
                //JOptionPane.showMessageDialog(rootPane, "i: "+i+" tms: "+tms.size());
                ArrayList<Aluno> alns = tms.get(i).getArrayAlunos();
                
                int k = 0;
                for(int j=0; j<alns.size(); j++){
                    Aluno a = alns.get(j);
                    String m1s = a.getNotaInString("M1S");
                    String m2s = a.getNotaInString("M2S");
                    String mf = a.getNotaInString("MF");
                    String r2s = "Não";
                    if(a.isRec2S())
                        r2s = "Sim";
                    String sit = "rp";
                    if(a.isAprovado())
                        sit = "ap";
                    String[] linha = {""+a.getNum(), a.getNome(), m1s, m2s, mf, r2s, sit};
                    hAlign[1] = Element.ALIGN_LEFT;
                    Font[] fnts = {fonte, fonte, fonte, fonte, bfonte, fonte, bfonte};
                    //JOptionPane.showMessageDialog(rootPane, "linha: "+linha.length+" vAlign: "+vAlign.length +" bgc: "+bgColor2.length+" border: "+border.length + " fnts: "+fnts.length);
                    if(k%2!=0)
                        pdf.addTableRow(linha, vAlign, hAlign, bgColor2, border, fnts);
                    else
                        pdf.addTableRow(linha, vAlign, hAlign, bgColor1, border, fnts);
                    if(j==alns.size()-1){
                        String[] vazio = {"","","","","","", ""};
                        int[] ultima = {PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP};
                        pdf.addTableRow(vazio, vAlign, hAlign, bgColor3, ultima, fnts);
                    }
                    k++;
                }
                pdf.addTale();
                pdf.createRodape();
            }
            pdf.close();
        }
    }
    
   /* private String floatToNota(float valor){
        BigDecimal r = new BigDecimal(valor);
        return r.setScale(1).toString().replace(".", ",");
    }*/
    
    private void gerarListasNotas(String string, String string0, String string1) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".pdf") || f.isDirectory();
            }

            public String getDescription() {
                return "Portable Document Format (.pdf)";
            }
        });

        BaseColor bc = new BaseColor(new Color(220, 220, 220));

        chooser.showSaveDialog(this);
        File nf = chooser.getSelectedFile();
        if (nf != null) {
            pdf = new Pdf(nf);
            ArrayList<Turma> tms = dados.getTurmas();
            for(int i=0; i<tms.size(); i++){
                pdf.addPage();
                pdf.setFontSize(16);
                pdf.setFontStyle(Font.BOLD);
                pdf.createCabecalho("Colégio Estadual Governador Valadares", "Química", "Jeferson", tms.get(i).getNome());
                pdf.setFontSize(10);
                pdf.setFontStyle(Font.NORMAL);
                int[] widths = {5, 75, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
                pdf.createTable(16, widths);
                String[] head = {"Nº", "NOME", "N1", "N2", "M1","R1", "MS1", "N3", "N4", "M2", "R2", "MS2", "M", "RF", "MF","SIT"};
                int[] vAlign = new int[16];//{Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER};
                int[] hAlign = new int[16];//{Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER, Element.ALIGN_CENTER};
                Font fonte = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL);
                Font bfonte = new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.BOLD);
                Font[] fts = new Font[16];//{bfonte, bfonte, bfonte, bfonte, bfonte, bfonte, bfonte};
                BaseColor[] bgc = new BaseColor[16];//{BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY, BaseColor.GRAY};
                BaseColor[] bgColor1 = new BaseColor[16];//{BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, bc, BaseColor.WHITE, BaseColor.WHITE};
                BaseColor[] bgColor3 = new BaseColor[16];//{BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE, BaseColor.WHITE};
                for(int k=0; k<16; k++){
                    vAlign[k] = Element.ALIGN_CENTER;
                    hAlign[k] = Element.ALIGN_CENTER;
                    fts[k] = bfonte;
                    bgc[k] = BaseColor.GRAY;
                    bgColor1[k] = BaseColor.WHITE;
                    bgColor3[k] = BaseColor.WHITE;
                }
                BaseColor[] bgColor2 = {bc, bc, bc, bc, BaseColor.LIGHT_GRAY, bc, BaseColor.LIGHT_GRAY, bc, bc, BaseColor.LIGHT_GRAY, bc, BaseColor.LIGHT_GRAY, BaseColor.LIGHT_GRAY, bc, BaseColor.LIGHT_GRAY, bc};
                int[] border = {PdfPCell.TOP, PdfPCell.BOX, PdfPCell.BOX, PdfPCell.BOX, PdfPCell.BOX, PdfPCell.BOX, PdfPCell.BOX,PdfPCell.BOX,PdfPCell.BOX,PdfPCell.BOX,PdfPCell.BOX,PdfPCell.BOX,PdfPCell.BOX,PdfPCell.BOX,PdfPCell.BOX,PdfPCell.TOP};
                pdf.addTableRow(head, vAlign, hAlign, bgc, border, fts);
                
                //JOptionPane.showMessageDialog(rootPane, "i: "+i+" tms: "+tms.size());
                ArrayList<Aluno> alns = tms.get(i).getArrayAlunos();
                
                int k = 0;
                for(int j=0; j<alns.size(); j++){
                    Aluno a = alns.get(j);
                    String n1 = a.getNotaInString("N1");
                    String n2 = a.getNotaInString("N2");
                    String rec = a.getNotaInString("R1");
                    String m1 = a.getNotaInString("M1S");
                    String n3 = a.getNotaInString("N3");
                    String n4 = a.getNotaInString("N4");
                    String rec2 = a.getNotaInString("R2");
                    String m2 = a.getNotaInString("M2S");
                    String recf = a.getNotaInString("RF");
                    String mf = a.getNotaInString("MF");
                    String sit = "rp";
                    if(a.isAprovado())
                        sit = "ap";
                    String[] linha = {""+a.getNum(), a.getNome(), n1, n2, a.getNotaInString("M1"), rec, m1, n3, n4, a.getNotaInString("M2"), rec2, m2, a.getNotaInString("MP"), recf, mf, sit};
                    hAlign[1] = Element.ALIGN_LEFT;
                    Font[] fnts = {fonte, fonte, fonte, fonte, bfonte, fonte, bfonte, fonte, fonte, bfonte, fonte, bfonte, bfonte, fonte, bfonte, fonte};
                    //JOptionPane.showMessageDialog(rootPane, "linha: "+linha.length+" vAlign: "+vAlign.length +" bgc: "+bgColor2.length+" border: "+border.length + " fnts: "+fnts.length);
                    if(k%2!=0)
                        pdf.addTableRow(linha, vAlign, hAlign, bgColor2, border, fnts);
                    else
                        pdf.addTableRow(linha, vAlign, hAlign, bgColor1, border, fnts);
                    if(j==alns.size()-1){
                        String[] vazio = {"","","","","","", "","","","","","","", "","", ""};
                        int[] ultima = {PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, 
                            PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP, PdfPCell.TOP};
                        pdf.addTableRow(vazio, vAlign, hAlign, bgColor3, ultima, fnts);
                    }
                    k++;
                }
                pdf.addTale();
            }
            pdf.close();
        }
    }
    
    private void addCommandButtond(String text, String icon, JRibbonBand band, RibbonElementPriority priority, ActionListener act){
        JCommandButton button = new JCommandButton(text, getResizableIconFromResource(icon));
        button.addActionListener(act);
        band.addCommandButton(button, priority);        
    }
    
    private void addCommandTogleButtond(String text, String icon, JRibbonBand band, RibbonElementPriority priority){
        JCommandToggleButton button = new JCommandToggleButton(text, getResizableIconFromResource(icon));
        band.addCommandButton(button, priority);
        //band.addRibbonGalleryButtons("teste", "teste", button);
        //JCommandButtonStrip bs = new JCommandButtonStrip();
        //bs.add(button);
    }

    private RibbonTask getConfiguracaoTask(){
        RibbonTask task1 = new RibbonTask("Configurações", getEscolaBand(), getTurmasBand(), getDisciplinaBand(), 
                getProfessoresBand(), getAlunosBand(), getAtividadesBand());
        return task1;
    }
    
    private JRibbonBand getEscolaBand(){
        JRibbonBand band = new JRibbonBand("Escola", null);
        ActionListener act = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                editEscolaButtonActionPerformed(e);
            }
        };
        addCommandButtond("Editar", "resources/edit.png", band, RibbonElementPriority.TOP, act);
        addCommandButtond("Regras", "resources/parameters_32.png", band, RibbonElementPriority.MEDIUM, null);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private void editEscolaButtonActionPerformed(ActionEvent evt){
        //AddEscola esc = new AddEscola(this, true);
        //esc.setVisible(true);
    }
    
    private JRibbonBand getTurmasBand(){
        JRibbonBand band = new JRibbonBand("Turmas", null);
        addCommandButtond("Adicionar", "resources/add_32.png", band, RibbonElementPriority.TOP, null);
        addCommandButtond("Editar", "resources/edit.png", band, RibbonElementPriority.MEDIUM, null);
        addCommandButtond("Remover", "resources/rem_32.png", band, RibbonElementPriority.MEDIUM, null);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private JRibbonBand getProfessoresBand(){
        JRibbonBand band = new JRibbonBand("Professores", null);
        addCommandButtond("Adicionar", "resources/add_32.png", band, RibbonElementPriority.TOP, null);
        addCommandButtond("Editar", "resources/edit.png", band, RibbonElementPriority.MEDIUM, null);
        addCommandButtond("Remover", "resources/rem_32.png", band, RibbonElementPriority.MEDIUM, null);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private JRibbonBand getDisciplinaBand(){
        JRibbonBand band = new JRibbonBand("Disciplinas", null);
        addCommandButtond("Adicionar", "resources/add_32.png", band, RibbonElementPriority.TOP, null);
        addCommandButtond("Editar", "resources/edit.png", band, RibbonElementPriority.MEDIUM, null);
        addCommandButtond("Remover", "resources/rem_32.png", band, RibbonElementPriority.MEDIUM, null);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private JRibbonBand getAlunosBand(){
        JRibbonBand band = new JRibbonBand("Alunos", null);
        ActionListener act = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                addAlunoButtonActionPerformed(e);
            }
        };
        addCommandButtond("Adicionar", "resources/add_32.png", band, RibbonElementPriority.TOP, act);
        ActionListener act2 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                remAlunoButtonActionPerformed(e);
            }
        };
        addCommandButtond("Editar", "resources/edit.png", band, RibbonElementPriority.TOP, null);
        addCommandButtond("Remover", "resources/rem_32.png", band, RibbonElementPriority.TOP, act2);
        
        ActionListener act1 = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                importAlunotxtButtonActionPerformed(e);
            }
        };
        addCommandButtond("Import (.txt)", "resources/import_32.png", band, RibbonElementPriority.MEDIUM, act1);
        addCommandButtond("Import (.pdf)", "resources/import_32.png", band, RibbonElementPriority.MEDIUM, null);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.Mid2Mid(band.getControlPanel()));
        //polices1.add(new IconRibbonBandResizePolicy(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private void addAlunoButtonActionPerformed(ActionEvent evt){
        AddAluno addAluno = new AddAluno(this, true, dados);
        addAluno.setVisible(true);
    }
    
    private void remAlunoButtonActionPerformed(ActionEvent evt){
        Turma t = (Turma)jComboBox2.getSelectedItem();
        int x = jXTable1.getSelectedRow();
        int n = (Integer)jXTable1.getModel().getValueAt(x, 0);
        //jXTable1.getSelectionModel().setSelectionInterval(x-1, x-1);
        jXTable1.getSelectionModel().clearSelection();
        t.remAluno(t.getAluno(n));
        jXTable1.repaint();
    }
    
    private void importAlunotxtButtonActionPerformed(ActionEvent evt){
        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".txt") || f.isDirectory();
            }

            public String getDescription() {
                return "Arquivos de Texto (.txt)";
            }
        });
        chooser.showOpenDialog(this);
        f = chooser.getSelectedFile();
        BufferedReader br;
        try {
            br = new BufferedReader(new FileReader(f));
            while(br.ready()){
                String s1 = br.readLine();
                if(s1.equals(""))
                    break;
                int s = Integer.parseInt(s1);
                String t = br.readLine();
                String linha = br.readLine();
                Turma turma = new Turma(s, t);
                int n = 1;
                while(!linha.equals("|")){
                    Aluno a = new Aluno(n, linha, turma);
                    n++;
                    turma.addAluno(a);
                    linha = br.readLine();
                }
                addTurma(turma);
            }
            br.close();
            this.repaint();
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private JRibbonBand getAtividadesBand(){
        JRibbonBand band = new JRibbonBand("Atividades", null);
        addCommandButtond("Adicionar", "resources/add_32.png", band, RibbonElementPriority.TOP, null);
        addCommandButtond("Editar", "resources/edit.png", band, RibbonElementPriority.MEDIUM, null);
        addCommandButtond("Remover", "resources/rem_32.png", band, RibbonElementPriority.MEDIUM, null);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private RibbonTask getViewTask(){
        RibbonTask task1 = new RibbonTask("Visualizar", getViewBand(), getSitAlunosBand());
        return task1;
    }
    
    private JRibbonBand getViewBand(){
        JRibbonBand band = new JRibbonBand("Visualização", null);
        addCommandTogleButtond("Notas", "resources/related_32.png", band, RibbonElementPriority.TOP);
        addCommandTogleButtond("Disciplinas", "resources/subjects.png", band, RibbonElementPriority.MEDIUM);
        addCommandTogleButtond("Professores", "resources/teachers.png", band, RibbonElementPriority.MEDIUM);
        addCommandTogleButtond("Turmas", "resources/class_32.png", band, RibbonElementPriority.MEDIUM);
        //JCommandToggleMenuButton x = new JCommandToggleMenuButton(null, null)
        
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private JRibbonBand getSitAlunosBand(){
        JRibbonBand band = new JRibbonBand("Alunos", null);
        addCommandButtond("Todos", "resources/summary_class.png", band, RibbonElementPriority.TOP, null);
        addCommandButtond("Aprovados", "resources/ungroup_48.png", band, RibbonElementPriority.MEDIUM, null);
        addCommandButtond("Reprovados", "resources/clearclasses_32.png", band, RibbonElementPriority.MEDIUM, null);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private void addRibbonTaskBar(){
        JCommandButton button = new JCommandButton("teste", getResizableIconFromResource("resources/clearclasses_32.png"));
        this.getRibbon().addTaskbarComponent(button);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        viewbuttongroup = new javax.swing.ButtonGroup();
        nimbusLookAndFeelAddons1 = new org.jdesktop.swingx.plaf.nimbus.NimbusLookAndFeelAddons();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jXList1 = new org.jdesktop.swingx.JXList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jXList2 = new org.jdesktop.swingx.JXList();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jXStatusBar1 = new org.jdesktop.swingx.JXStatusBar();
        jXBusyLabel1 = new org.jdesktop.swingx.JXBusyLabel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXTitledPanel1 = new org.jdesktop.swingx.JXTitledPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();

        jXLabel1.setText("Química");

        jXLabel2.setText("1ºA");

        jXList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Química", "Física" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jXList1);

        jXList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "1ºA", "1ºB" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jXList2);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Química", "Física" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jComboBox2.setModel(tcbm);
        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox2ItemStateChanged(evt);
            }
        });

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jXBusyLabel1.setText("jXBusyLabel1");
        jXStatusBar1.add(jXBusyLabel1);

        getContentPane().add(jXStatusBar1, java.awt.BorderLayout.PAGE_END);

        jXPanel1.setLayout(new java.awt.BorderLayout());

        jXTitledPanel1.setLeftDecoration(jComboBox1);
        jXTitledPanel1.setRightDecoration(jComboBox2);
        jXTitledPanel1.setTitle("");
        jXTitledPanel1.getContentContainer().setLayout(new java.awt.BorderLayout());

        jXTable1.setModel(ttm);
        jScrollPane1.setViewportView(jXTable1);

        jXTitledPanel1.getContentContainer().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jXPanel1.add(jXTitledPanel1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jXPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox2ItemStateChanged
    Turma t = (Turma) jComboBox2.getSelectedItem();
    ttm.setTurma(t);
    jXTable1.repaint();
}//GEN-LAST:event_jComboBox2ItemStateChanged

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
    /*Disciplina d = (Disciplina)jComboBox1.getSelectedItem();
    ttm.setDisciplina(d);
    jXTable1.repaint();*/
}//GEN-LAST:event_jComboBox1ItemStateChanged

    private RibbonTask getRelatorioTask(){
        RibbonTask task1 = new RibbonTask("Relatório", getPresencaBand(), getSitAlunosBand());
        return task1;
    }
    
    private JRibbonBand getPresencaBand(){
        JRibbonBand band = new JRibbonBand("Lista de Presença", null);
        ActionListener act = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //gerarListasPresenca(e);
            }
        };
        addCommandButtond("Lista de Presença", "resources/related_32.png", band, RibbonElementPriority.TOP, act);
        ArrayList polices1 = new ArrayList();
        polices1.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
        band.setResizePolicies(polices1);
        return band;
    }
    
    private void gerarListasPresenca(String colName, String prof, String motivo){
        try {
            ArrayList<Turma> tms = dados.getTurmas();
            Turma t = tms.get(0);
            File fp = new File(t.getNome()+".pdf");
            FileOutputStream fos = new FileOutputStream(fp);

            Document doc = new Document(PageSize.A4, 20, 20, 15, 1);
            PdfWriter.getInstance(doc, fos);
            doc.open();
            doc.addTitle("Alunos");
            final int col = 4;
            final int lines = 45;
            for (int i = 0; i < tms.size(); i++) {
                doc.newPage();
                t=tms.get(i);
                Chunk c = new Chunk("GOVERNO DE SERGIPE", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                Paragraph parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("SECRETARIA DE ESTADO DA EDUCAÇÃO", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk(colName, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                //c = new Chunk("FREQUÊNCIA DA "+motivo+" DA TURMA " + t.getNome(), new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                c = new Chunk(motivo+" DA TURMA " + t.getNome(), new Font(Font.FontFamily.TIMES_ROMAN, 14, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                /*c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);*/
                //Turma t = turmas.get(i);
                c = new Chunk("Professor: "+prof, new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                /*c = new Chunk("APLICADOR:___________________________________________________ DATA: ___/___/______"
                        , new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);*/
                c = new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                ArrayList<Aluno> alunos = t.getArrayAlunos();
                float[] l = new float[col];
                l[0] = 2f;
                l[1] = 32f;//22
                l[2] = 30f;
                l[3] = 5f;
                /*float r = 75f/col;
                for(int j=2; j<col; j++){
                    l[j] = r;
                }*/
                PdfPTable table = new PdfPTable(l);
                table.setWidthPercentage(100f);
                Phrase p = new Phrase(new Chunk("Nº", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                PdfPCell cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                //cell.setBorder(PdfPCell.TOP);
                //cell.setBorder(PdfPCell.BOTTOM);
                //cell.setBorder(PdfPCell.RIGHT);
                cell.setBorder(PdfPCell.TOP);
                //cell.setBorder(Rectangle.)
                //cell.setRowspan(2);
                table.addCell(cell);
                p = new Phrase(new Chunk("ALUNO", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                //cell.setRowspan(2);
                table.addCell(cell);
                p = new Phrase(new Chunk("ASSINATURA", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                //cell.setRowspan(2);
                //cell.setColspan(col-2);
                table.addCell(cell);
                p = new Phrase(new Chunk("NOTA", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD)));
                cell = new PdfPCell(p);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                //cell.setRowspan(2);
                //cell.setColspan(col-2);
                cell.setBorder(PdfPCell.TOP);
                table.addCell(cell);
                for (int j = 0; j < lines; j++) {
                    if (j < alunos.size()) {
                        p = new Phrase(new Chunk(""+alunos.get(j).getNum(), new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setBorder(PdfPCell.TOP);
                        table.addCell(cell);
                        p = new Phrase(new Chunk(alunos.get(j).getNome(), new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        //cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        table.addCell(cell);
                    } else {
                        p = new Phrase(new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setBorder(PdfPCell.TOP);
                        table.addCell(cell);
                        p = new Phrase(new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                        cell = new PdfPCell(p);
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        if(j==lines-1)
                            cell.setBorder(PdfPCell.TOP);
                        table.addCell(cell);
                    }
                    
                    p = new Phrase(new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 10)));
                    cell = new PdfPCell(p);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    if(j==lines-1)
                        cell.setBorder(PdfPCell.TOP);
                    //p = new Phrase(new Chunk(" ", new Font(Font.FontFamily.TIMES_ROMAN, 8)));
                    for (int k = 0; k < col - 3; k++) {
                        table.addCell(cell);
                    }
                    cell = new PdfPCell(p);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell.setBorder(PdfPCell.TOP);
                    table.addCell(cell);
                }
                doc.add(table);
                /*c = new Chunk("Observações:___________________________________________________________________"
                        + "_______________________________________________________________________________", new Font(Font.FontFamily.TIMES_ROMAN, 10));
                parag = new Paragraph(c);
                doc.add(parag);
                c = new Chunk("                   _____________________________________________               _____________________________________________", new Font(Font.FontFamily.TIMES_ROMAN, 10));
                parag = new Paragraph(c);
                //parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);
                c = new Chunk("                         Assinatura do Responsável - Comitê Pedagógico                                                  Assinatura da Direção            ", new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.BOLD));
                parag = new Paragraph(c);
                //parag.setAlignment(Element.ALIGN_CENTER);
                doc.add(parag);*/
            }
            doc.close();
        } catch (DocumentException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static ResizableIcon getResizableIconFromResource(String resource){
        return ImageWrapperResizableIcon.getIcon(MainWindow.class.getClassLoader().getResource(resource), new Dimension(64, 64));
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
                //
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
            
        

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                
                new MainWindow().setVisible(true);
            }
        });        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private org.jdesktop.swingx.JXBusyLabel jXBusyLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXList jXList1;
    private org.jdesktop.swingx.JXList jXList2;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXStatusBar jXStatusBar1;
    private org.jdesktop.swingx.JXTable jXTable1;
    private org.jdesktop.swingx.JXTitledPanel jXTitledPanel1;
    private org.jdesktop.swingx.plaf.nimbus.NimbusLookAndFeelAddons nimbusLookAndFeelAddons1;
    private javax.swing.ButtonGroup viewbuttongroup;
    // End of variables declaration//GEN-END:variables
}
